<?php 
namespace School\Controller;

use Think\Controller;
use Common\Model;

class IndexController extends Controller{
	
	public $uid;
	//
	public $type_name=array(1=>"科目一",2=>"科目二",3=>"科目三",4=>"科目四",5=>"科目五");
	public function __construct(){
		parent::__construct();
	}
	//代理商绑定用户
	public function PartnerLogin(){
		
		$this->uid=wx_login();
		//$this->uid=5;
		//判断是否绑定账号
		$map['id']=$this->uid;
		$map['level']=2;
		$r=M("student_wx")->where($map)->find();
		
		if($r){
			redirect(U("PartnerCenter"));exit;
		}
		if(IS_AJAX){
			$mobile=I("phone");
			$checkcode=I("checkcode");
			$password=I("password");
			if(!$checkcode||$checkcode!=session("checkcode")){
				//$this->success("成功绑定",U("PartnerCenter"));
				$this->error("短信验证码错误或已超时");
			}
			if(!$mobile||!$password){
				$this->error("请输入手机或者密码");
			}
			$map=array();
			$map['mobile']=$mobile;
			$user=M("partner")->where($map)->find();
			if(!$user){
				$this->error("您绑定的合作商账号不存在");
			}
			//判断账号密码
			if(encrypt($password)!=$user['password']){
				$this->error("账号或密码错误");
			}
			
			$r=M("student_wx")->where("id='".$this->uid."'")->save(array("level"=>2));
			$r=M("partner")->where($map)->save(array("wxid"=>$this->uid));
			if($r){
				$this->success("成功绑定",U("PartnerCenter"));
			}else{
				$this->error("绑定失败或已绑定");
			}
		}else{
			$this->display();
		}
	}
	//发送验证码
	public function sendCheckCode(){
		
		$phone=I("phone");
		if(!$phone){
			$this->error("请输入正确的手机号码");
		}
		$sms=D("Message");
		$code=$sms->Msend($phone);
		session("checkcode",$code);
		$this->success("发送成功");
	}
	//代理商中心
	public function PartnerCenter(){
		
		$this->uid=wx_login();
		//$this->uid=5;
		$uid=$this->uid;
		//$uid=5;
		$map=array();
		$map['id']=$uid;
		$user=M("student_wx")->where($map)->find();
		
		//判断是否已经绑定渠道商
		if($user["level"]!=2){
			redirect(U("PartnerLogin"));
		}
		//获取用户数据
		
		$map['id']=$uid;
		$wxInfo=M("student_wx")->where($map)->find();
		$map=array();
		$map['wxid']=$uid;
		$partnerInfo=M("partner")->where($map)->find();
		
		$count=M("student_wx")->where("channelNo='".$partnerInfo['channelNo']."'")->count();
		
		$this->assign("wxInfo",$wxInfo);
		$this->assign("count",$count);
		$this->assign("partnerInfo",$partnerInfo);
		$this->display();
	}
	//我的二维码
	public function myShareEwm(){
		$this->uid=wx_login();
		
		$myInfo=M("partner")->where("wxid='".$this->uid."'")->find();
		$channelNo=$myInfo['channelNo'];
		
		$imgEwmUrl="http://".$_SERVER['HTTP_HOST']."/index.php?s=/School/index/channelQrcode/channelNo/".$channelNo;
		
		$this->assign("ewmUrl",$imgEwmUrl);
		$this->display();
	}
	//二维码图片地址
	public function showqrcode(){
		ob_clean();
		Vendor('phpqrcode.phpqrcode');
		$uri=base64_decode(I('uri'));
		$errorCorrectionLevel = "L"; // 纠错级别：L、M、Q、H
		$matrixPointSize = "4"; // 点的大小：1到10
		$object = new \QRcode();
		echo $object->png($uri, false, $errorCorrectionLevel, $matrixPointSize, 2);
		exit;
	}
	//渠道二维码地址
	public function channelQrcode(){
		ob_clean();
		Vendor('phpqrcode.phpqrcode');
		$channelNo=I("channelNo");
		$uri="http://".$_SERVER['HTTP_HOST'].U("inviteeRegister",array('channelNo'=>$channelNo));
		$errorCorrectionLevel = "L"; // 纠错级别：L、M、Q、H
		$matrixPointSize = "4"; // 点的大小：1到10
		$object = new \QRcode();
		echo $object->png($uri, false, $errorCorrectionLevel, $matrixPointSize, 2);
		exit;
	}
	//客户扫码跳转注册页面
	public function inviteeRegister(){
			$this->uid=wx_login();
			//$this->uid=5;
			$channelNo=I("channelNo");
		if(IS_AJAX){
			$mobile=I("phone");
			$checkcode=I("checkcode");
			//if(!$checkcode||$checkcode!=session("checkcode")){
				//$this->error("短信验证码错误或已超时");
			//}
			if(!$mobile){
				$this->error("请输入手机或者密码");
			}
			$map=array();
			$map['mobile']=$mobile;
			$map['uid']=$this->uid;
			$user=M("student_wx")->where($map)->find();
			
			if(!$user){
				$map=array();
				$map['id']=$this->uid;
				$r=M("student_wx")->where($map)->save(array("mobile"=>$mobile,"channelNo"=>$channelNo));
				//增加优惠券
				if($channelNo){
					$r=D("Coupon")->inviteAdd($this->uid,$channelNo);
				}
				if($r){
					$this->success("已获得优惠券",U("myCenter"));
				}else{
					$this->error("系统错误!");
				}
			}else{
				$this->error("您已经领取优惠券，请在个人中心查看");
			}
		}else{
			$this->assign("channelNo",$channelNo);
			$this->display();
		}
	}
	//用户个人中心
	public function myCenter(){
		$this->uid=wx_login();
		//$this->uid=5;
		$user=M("student_wx")->find($this->uid);
		
		$this->assign("userInfo",$user);
		$this->display();
	}
	//我的订单
	public function myOrder(){
		//$this->uid=wx_login();
		$this->uid=5;
		$list=M("trade")->where("uid='".$this->uid."'")->page(1,15)->order("createtime desc")->select();
		
		foreach($list as $k=>$v){
			
			$course=M("course a")->join("__PICTURE__ b on a.img=b.id","left")->field("a.*,b.path")->where("a.id='".$v['course_id']."'")->order("top desc,addtime desc")->find();
			$list[$k]['course_img']=$course['path'];
		}
		$this->assign("host","http://".$_SERVER['HTTP_HOST'].__ROOT__);
		$this->assign("_list",$list);
		$this->display();
	}
	
	//课程列表
	public function signup(){
		
		$list=M("course a")->join("__PICTURE__ b on a.img=b.id","left")->field("a.*,b.path")->where($map)->order("top desc,addtime desc")->select();
		$this->assign("host","http://".$_SERVER['HTTP_HOST'].__ROOT__);
		
		$this->assign("_list",$list);
		$this->display();
		
	}
	//课程详情
	public function courseDetail(){
		$map['a.id']=I("id");
		$course=M("course a")->join("__PICTURE__ b on a.img=b.id","left")->field("a.*,b.path")->where($map)->find();
		$this->assign("host","http://".$_SERVER['HTTP_HOST'].__ROOT__);
		$this->assign("_course",$course);
		$this->display();
	}
	//报名课程
	public function pickCourse(){
		//$this->uid=wx_login();
		$this->uid=5;
		
		if(!I("id")){
			redirect(U("signup"));
		}
		$map['a.id']=I("id");
		
		$course=M("course a")->join("__PICTURE__ b on a.img=b.id","left")->field("a.*,b.path")->where($map)->find();
		
		//查看是否有优惠券
		$coupon=D("Coupon")->getInviteCoupon($this->uid,$id);
		if($coupon){
			$this->assign("coupon",$coupon);
		}
		$this->assign("course",$course);
		$this->assign("host","http://".$_SERVER['HTTP_HOST'].__ROOT__);
		$this->display();
	}
	//下单
	public function tradeOrder(){
		$this->uid=wx_login();
		//$this->uid=5;
		if(!$this->uid){
			$this->error("未获取到用户信息,请重新登录",U("inviteeRegister"));
		}
		//如果没录入学员信息，就必须让他们填写
		$info=M("student_info")->where("uid='".$this->uid."'")->find();
		
		if(!$info){
			$this->error("必须填写学员信息才能提交订单！");
		}
		
		$course_id=I("course_id");
		$coupon_id=I("coupon_id");
		if(!$course_id){
			$this->error("请选择课程再来",U("signup"));
		}
		if(IS_AJAX){
			$r=D("Trade")->orderAdd($this->uid,$course_id,$coupon_id);
			if($r){
				$this->success("下单成功",U("payNext",array("tradeid"=>$r)));
			}else{
				$this->error("下单失败");
			}
		}
	}
	//报名填写个人信息
	public function studentInfo(){
	
		//$uid=I("uid");
		$uid=5;
		$courseid=I("courseid");
		if(!$uid){
			$this->uid=wx_login();
			$uid=$this->uid;
		}
		$info=M("student_info")->where("uid='".$uid."'")->find();
		
		if(IS_POST){
			
			//验证码
			$mobile=I("mobile");
			$checkcode=I("checkcode");
			if(!$checkcode||$checkcode!=session("checkcode")){
				$this->error("短信验证码错误或已超时");
			}
			if(!$mobile){
				$this->error("请输入手机或者密码");
			}
			
			$param['uid']=$uid;
			$param['name']=I("username");
			$param['identify']=I("identify");
			$param['identify_addr']=I("identify_addr");
			$param['gender']=I("gender");
			$param['have_mb']=I("have_mb");
			$param['join_school']=I("join_school");
			$param['mobile']=I("mobile");
			$param['height']=I("height");
			$param['is_shortsignted']=I("is_shortsignted");
			$param['address']=I("address");
			$param['contract']=0;
			$param['addtime']=date("Y-m-d H:i:s",time());
			
			if(!$info){
				$r=M("student_info")->add($param);
			}else{
				unset($param['uid']);
				$r=M("student_info")->where("uid='".$info['uid']."'")->save($param);
			}
			
			if($r){
				$this->messageSend("录入成功",U("pickCourse",array('id'=>I("courseid"))));
			}else{
				$this->messageSend("录入失败",U("studentInfo"));
			}
		}else{
			$this->assign("courseid",$courseid);
			$this->assign("uid",$uid);
			$this->assign("info",$info);
			
			$this->display();
		}
	}
	//提示页面
	public function messageSend($msg="",$url=""){
		
		if(!$msg){
			$msg="提示信息";
		}
		if(!$url){
			$url=U("signup");
		}
		$this->assign("msg",$msg);
		$this->assign("url",$url);
		$this->display("messageSend");
	}
	//线下支付
	public function payNext(){
		$tradeid=I("tradeid");
		if(IS_AJAX){
			$param['paytype']=0;
			$r=M("trade")->where("id='".$tradeid."'")->save($param);
			
			if($r){
				$this->success("下单成功，",U("tradeComplete"));
			}else{
				$this->error("下单失败/已支付",U("myCenter"));
			}
		}else{
			$this->assign("tradeid",$tradeid);
			$this->display();
		}
	}
	//支付完成
	public function tradeComplete(){
		
		$this->display();
	}
	//电子订单
	public function contract(){
		$this->uid=wx_login();
		//$this->uid=5;
		if(IS_POST){
			$param['contract']=1;
			$r=M("student_info")->where("uid='".$this->uid."'")->save($param);
			if($r){
				$this->success("签署成功");
			}else{
				$this->error("已签署");
			}
		}else{
		   $this->display();
		}
	}
	//考试中心
	public function examCenter(){
		$this->uid=wx_login();
		//$this->uid=5;
		//获取可报名的信息
		$map['status']=1;
		$map['enterEndDate']=array("gt",date("Y-m-d H:i:s",time()));
		$join_list=M("exam")->where($map)->select();
		$join=array();
		$joined=array();
		
		$exam=M("exam_student")->where("uid='".$this->uid."'")->select();
		
		$examArr=array();
		foreach($exam as $k=>$v){
			$examArr[]=$v['examid'];
		}
		
		foreach($join_list as $k =>$v){
			$join_list[$k]['type']=$this->type_name[$v['type']];
			
			if(in_array($v['id'],$examArr)){
				$joined[]=$join_list[$k];
			}else{
				$join[]=$join_list[$k];
			}
			
		}
		
		$this->assign("join_list",$join);
		$this->assign("joined_list",$joined);
		$this->display();
	}
	//考试详情
	public function examInfo(){
		$this->uid=wx_login();
		//$this->uid=5;
		$id=I("id");
		$exam=M("exam")->find($id);
		
		$exam['type']=$this->type_name[$exam['type']];
		$count=M("exam_student")->where("examid=".$id)->count();
		
		$this->assign("exam",$exam);
		$this->assign("count",$count);
		$this->assign("uid",$this->uid);
		$this->display();
	}
	//已报名考试详情
	public function examInfoJoin(){
		$this->uid=wx_login();
		//$this->uid=5;
		$id=I("id");
		$exam=M("exam")->find($id);
		
		$count=M("exam_student")->where("examid=".$id)->count();
		
		$exam['type']=$this->type_name[$exam['type']];
	
		$this->assign("exam",$exam);
		$this->assign("count",$count);
		$this->assign("uid",$this->uid);
		$this->display();
	}
	//考试报名
	public function examJoin(){
		$post=I();
		if(!$post['uid']){
			$this->error("缺少id");
		}
		if(!$post['examid']){
			$this->error("缺少考试id");
		}
		$r=M("exam_student")->where($post)->find();
		
		//如果考试人数满了 无法报名
		$count=M("exam_student")->where("examid=".$post['examid'])->count();
		$exam=M("exam")->find($post['examid']);
		if($count>=$exam['enterNum']){
			$this->success("考试报名人数已满",U("examCenter"));
		}
		
		if($r){
			$this->success("您已经报名，无需重复报名",U("examCenter"));
		}else{
			$post['addtime']=date("Y-m-d H:i:s",time());
			$z=M("exam_student")->add($post);
			if($z){
				$this->success("报名成功",U("examCenter"));
			}
		}
	}
	//合作-- 我的收益
	public function myIncome(){
		$this->uid=wx_login();
		//$this->uid=6;
		
		$p=D("Partner")->getPartnerByWX($this->uid);
		$wxcount=D("Partner")->getWxCount($this->uid);
		$studentcount=D("Partner")->getStudentCount($this->uid);
		$this->assign("p",$p);
		$this->assign("wx",$wxcount);
		$this->assign("student",$studentcount);
		$this->display();
	}
	//合作-- 我的意向客户信息
	public function myWxuser(){
		$this->uid=wx_login();
		//$this->uid=6;
		$list=D("Partner")->WxList($this->uid);
		$this->assign("list",$list);
		
		$this->display();
	}
	//合作-- 我的意向客户信息
	public function myStudent(){
		$this->uid=wx_login();
		//$this->uid=6;
		$list=D("Partner")->StudentList($this->uid);
		
		$this->assign("list",$list);
		$this->display();
	}
}
?>